package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.Deliverypoint;
import com.zr.model.User;
import com.zr.service.DeliverypointServiceLPN;
import com.zr.service.DistributionscopeServiceLPN;

import net.sf.json.JSONArray;
/**
 * 配送间范围
 * @author 李培宁
 *
 */
public class DistributionscopeActionLPN extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware{
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private Map<String, Object> session;
	private int dpId;
	private int page;
	private int rows;
	private String dsName;
	private String dsRemarks;
	private String dsDatestart;
	private String dsDatestop;
	private int dsId;
	
	
	
	
	public int getDsId() {
		return dsId;
	}
	public void setDsId(int dsId) {
		this.dsId = dsId;
	}
	public String getDsDatestart() {
		return dsDatestart;
	}
	public void setDsDatestart(String dsDatestart) {
		this.dsDatestart = dsDatestart;
	}
	public String getDsDatestop() {
		return dsDatestop;
	}
	public void setDsDatestop(String dsDatestop) {
		this.dsDatestop = dsDatestop;
	}
	public String getDsName() {
		return dsName;
	}
	public void setDsName(String dsName) {
		this.dsName = dsName;
	}
	public String getDsRemarks() {
		return dsRemarks;
	}
	public void setDsRemarks(String dsRemarks) {
		this.dsRemarks = dsRemarks;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public int getDpId() {
		return dpId;
	}
	public void setDpId(int dpId) {
		this.dpId = dpId;
	}
	//属性注入
	@Resource
	private   DistributionscopeServiceLPN dss;
	
	
	/**
	 * 得到配送间范围根据配送点
	 * @throws IOException
	 */
	public void getDS() throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		JSONArray j = dss.findDS(dpId);
		PrintWriter pw = resp.getWriter();
		pw.write(j.toString());
	}
	
	
	
	/**
	 * 得到配送间范围根据用户id
	 * @throws IOException
	 */
	public void getDSByUid() throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		User u=(User) session.get("user");
		JSONArray j = dss.getDSByUid(page, rows, u.getUid());
		PrintWriter pw = resp.getWriter();
		pw.write(j.toString());
		pw.flush();
		pw.close();
	}
	
	
	/**
	 * 得到所有的配送范围
	 * @throws IOException 
	 */
	public void getAllDS() throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		JSONArray j = dss.getAllDS(page, rows);
		PrintWriter pw = resp.getWriter();
		pw.write(j.toString());
		pw.flush();
		pw.close();
	}
	
	
	
	
	
	
	/**
	 * 增加配送间范围
	 * @throws IOException
	 */
	public void insertDS() throws IOException{
		String dsDate= dsDatestart+"-"+dsDatestop;
		boolean flag = dss.insertDS(dsName, dsRemarks, dpId, dsDate);
		PrintWriter pw = resp.getWriter();
		if (flag) {
			pw.write("1");
		} else {
			pw.write("0");
		}
		pw.flush();
		pw.close();
	}
	
	
	/**
	 * 增加配送间范围验证
	 * @throws IOException 
	 */
	public void validateAddDs() throws IOException{
		int i = dss.finddsIdBydpIdAndsName(dpId, dsName);
		PrintWriter pw = resp.getWriter();
		if (i>0) {
			pw.write("1");
		} else {
			pw.write("0");
		}
		pw.flush();
		pw.close();
	}
	
	/**
	 * 修改配送范围
	 * @throws IOException 
	 */
	public void updateDs() throws IOException{
		System.out.println(222222222);
		String dsDate= dsDatestart+"-"+dsDatestop;
		int i = dss.updateDS(dsId, dsName, dsRemarks, dsDate);
		PrintWriter pw = resp.getWriter();
		if (i>0) {
			pw.write("1");
		} else {
			pw.write("0");
		}
		pw.flush();
		pw.close();
	}
	
	@Override
	public String execute() throws Exception {
		System.out.println(333333333);
		return null;
	}
	
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.resp=arg0;
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.req=arg0;
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.session=arg0;
	}
}
